# Maintainer: Rafał Brzegowy <rafal.brzegowy@yahoo.com>

_realname=seacas
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2023.05.30
_releases=${pkgver//./-}
pkgrel=2
pkgdesc="The Sandia Engineering Analysis Code Access System (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
conflicts=("${MINGW_PACKAGE_PREFIX}-libexodus")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-metis"
         "${MINGW_PACKAGE_PREFIX}-matio"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
url="https://gsjaardema.github.io/seacas/"
license=('spdx:BSD-3-Clause')
source=("https://github.com/gsjaardema/seacas/archive/v${_releases}.tar.gz"
        "0001-cmake-add-libs.patch"
        "0002-cmake-findHDF5.patch")
sha256sums=('3dd982841854466820a3902163ad1cf1b3fbab65ed7542456d328f2d1a5373c1'
            'a3561756bfdeb942fc62add0aefa542b0e689b76a7f0d2d357e299465ee344d2'
            '09c4406d57c5ca18b0d2f16161dd6f5e2c5d56f0f73e83bd77272d876569d5cc')
noextract=(v${_releases}.tar.gz)

prepare() {
  plain "Extract v${_releases}.tar.gz"
  tar -xzf v${_releases}.tar.gz || true

  cd "${srcdir}/${_realname}-${_releases}"
  patch -p1 -i "${srcdir}/0001-cmake-add-libs.patch"
  patch -p1 -i "${srcdir}/0002-cmake-findHDF5.patch"
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  #Static Build
  mkdir -p "${_realname}-${_releases}/build-${MSYSTEM}-static" && cd "${_realname}-${_releases}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      INSTALL_PATH=${MINGW_PREFIX} THREADSAFE=ON ../cmake-config \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${extra_config[@]} \
      -DCMAKE_C_COMPILER=${MINGW_PREFIX}/bin/${CC}.exe \
      -DCMAKE_CXX_COMPILER=${MINGW_PREFIX}/bin/${CXX}.exe \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DSeacas_ENABLE_Fortran=OFF \
      -DTPL_ENABLE_X11=OFF \
      -DSeacas_ENABLE_TESTS=OFF \
      -DBUILD_SHARED_LIBS=OFF

  ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  mkdir -p "../build-${MSYSTEM}-shared" && cd "../build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      INSTALL_PATH=${MINGW_PREFIX} THREADSAFE=ON ../cmake-config \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${extra_config[@]} \
      -DCMAKE_C_COMPILER=${MINGW_PREFIX}/bin/${CC}.exe \
      -DCMAKE_CXX_COMPILER=${MINGW_PREFIX}/bin/${CXX}.exe \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DSeacas_ENABLE_Fortran=OFF \
      -DTPL_ENABLE_X11=OFF \
      -DSeacas_ENABLE_TESTS=OFF \
      -DBUILD_SHARED_LIBS=ON

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/${_realname}-${_releases}/build-${MSYSTEM}-static"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  #Shared Install
  cd "${srcdir}/${_realname}-${_releases}/build-${MSYSTEM}-shared"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}/${_realname}-${_releases}"/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in $(find "${pkgdir}${MINGW_PREFIX}"/lib/cmake -type f); do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  #remove useless file, a "fake" mpi interface that allows us to use MPI-enabled codes/libraries (e.g. zoltan) in an application that is built without MPI, conflict with msmpi:
  rm -r ${pkgdir}${MINGW_PREFIX}/include/mpi.h
  #rm -r ${pkgdir}${MINGW_PREFIX}/include/_MPI_UTILITY.h
  #rm -r ${pkgdir}${MINGW_PREFIX}/include/mpi*.h
  #rm -r ${pkgdir}${MINGW_PREFIX}/lib/libsimpi.a
}
